package com.fattyCorps.common.packet.hero.component.skill;

import cn.hutool.extra.spring.SpringUtil;
import com.fattyCorps.common.enums.battle.ESkillType;
import com.fattyCorps.common.mgr.Cfg;
import com.fattyCorps.common.obj.config.SkillMould;
import com.fattyCorps.common.packet.hero.component.skill.component.ABaseSkillComponent;
import com.fattyCorps.common.packet.hero.component.skill.component.BirthSkill;
import com.fattyCorps.common.packet.hero.component.ABaseHeroComponent;
import com.fattyCorps.common.packet.hero.component.Component;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 技能组件
 * 计算当前英雄所拥有的所有技能，包含天生技能与其它系统附加技能
 * 其它系统需要继承IComplonent后将自身系统的技能加入到技能组件
 */
public class HeroSkill extends ABaseHeroComponent {
    Cfg cfg;

    @Getter
    Component.Trunk trunk = new Component.Trunk(this);

    List<SkillMould> skills = new ArrayList<>();

    @Override
    public void onInit() {
        cfg = SpringUtil.getBean(Cfg.class);
        trunk.addComponent(BirthSkill.class);
    }

    @Override
    public void onUpdate() {
        skills.clear();
        trunk.onUpdate();

        Iterator<Map.Entry<Class, Component.IComponent>> iterator = trunk.getComponents().entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Class, Component.IComponent> next = iterator.next();
            ABaseSkillComponent skillComp = (ABaseSkillComponent) next.getValue();
            addSkillAll(skillComp.getSkills());
        }
    }

    @Override
    public void onEvent(int e, Object[] objs) {

    }

    public void addSkillAll(List<SkillMould> skillsAdd) {
        skills.addAll(skillsAdd);
    }

    // 获取所有属性相关技能
    public List<SkillMould> getPropSkills() {
        List<SkillMould> retList = new ArrayList<>();

        for (SkillMould skill : skills) {
            if (skill.getType().equals(ESkillType.Prop)) {
                retList.add(skill);
            }
        }

        return retList;
    }

    // 获取所有技能
    public List<SkillMould> getAllSkills() {
        return skills;
    }
}
